package com.qm.utils;

import android.util.Log;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Author by Winds on 2016/10/18.
 * Email heardown@163.com.
 */
public class ByteUtil {

    public static byte[] merge(byte[] bytes1, byte[] bytes2) {
        byte[] ret = new byte[bytes1.length + bytes2.length];
        System.arraycopy(bytes1, 0, ret, 0, bytes1.length);
        System.arraycopy(bytes2, 0, ret, bytes1.length, bytes2.length);
        return ret;
    }

    public static byte getXor(byte[] bytes, int len) {
        byte ret = 0;

        for (int i = 0; i < len; i++) {
//            byte tmp = datas[i] &
//                    CRC ^= (int) bytes[i]&0x000000ff;
            ret ^= bytes[i];
        }

        return ret;
    }

    public static byte getBCC(byte[] data, int len) {
        byte[] BCC = new byte[1];
        for (int i = 0; i < len; i++) {
            BCC[0] = (byte) (BCC[0] ^ data[i]);
        }
        return BCC[0];
    }
    public static byte getBCC(byte[] ret) {
        byte [] data=new byte[6];
        System.arraycopy(ret, 1, data, 0, data.length);
        byte[] BCC = new byte[1];
        for (byte datum : data) {
            BCC[0] = (byte) (BCC[0] ^ datum);
        }
        return BCC[0];
    }
    public static byte getBCCFinger(byte[] ret,int len) {
        byte [] data=new byte[len];
        System.arraycopy(ret, 1, data, 0, data.length);
        byte[] BCC = new byte[1];
        for (byte datum : data) {
            BCC[0] = (byte) (BCC[0] ^ datum);
        }
        return BCC[0];
    }
    public static String bytes2HexStr(byte[] src) {
        StringBuilder builder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return "";
        }
        char[] buffer = new char[2];
        for (int i = 0; i < src.length; i++) {
            buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);
            buffer[1] = Character.forDigit(src[i] & 0x0F, 16);
            builder.append(buffer);
        }
        return builder.toString().toUpperCase();
    }

    public static String bytes2HexStr(byte[] src, int dec, int length) {
        byte[] temp = new byte[length];
        System.arraycopy(src, dec, temp, 0, length);
        return bytes2HexStr(temp);
    }

    public static long hexStr2decimal(String hex) {
        return Long.parseLong(hex, 16);
    }

    public static String decimal2fitHex(long num) {
        String hex = Long.toHexString(num).toUpperCase();
        if (hex.length() % 2 != 0) {
            return "0" + hex;
        }
        return hex.toUpperCase();
    }

    public static String decimal2fitHex(long num, int strLength) {
        String hexStr = decimal2fitHex(num);
        StringBuilder stringBuilder = new StringBuilder(hexStr);
        while (stringBuilder.length() < strLength) {
            stringBuilder.insert(0, '0');
        }
        return stringBuilder.toString();
    }

    public static String fitDecimalStr(int dicimal, int strLength) {
        StringBuilder builder = new StringBuilder(String.valueOf(dicimal));
        while (builder.length() < strLength) {
            builder.insert(0, "0");
        }
        return builder.toString();
    }

    public static String str2HexString(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder();
        byte[] bs = null;
        try {

            bs = str.getBytes("utf8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
        }
        return sb.toString();
    }

    public static byte[] time2HexBytes(Date date) {
        String dtStr = new SimpleDateFormat("yyMMddHHmmss").format(date);
        byte[] tmp = ByteUtil.decStr2bytes(dtStr);
        return tmp;
    }

    public static byte[] intToBytes(int n) {
        byte[] b = new byte[2];
        b[1] = (byte) (n & 0xff);
        b[0] = (byte) (n >> 8 & 0xff);
        return b;
    }

    public static byte[] decStr2bytes(String dec) {
        int len = (dec.length() / 2);
        byte[] result = new byte[len];
        char[] achar = dec.toUpperCase().toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (hexChar2byte(achar[pos]) * 10 + hexChar2byte(achar[pos + 1]));
        }
        return result;
    }

    public static byte[] hexStr2bytes(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toUpperCase().toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (hexChar2byte(achar[pos]) << 4 | hexChar2byte(achar[pos + 1]));
        }
        return result;
    }

    private static int hexChar2byte(char c) {
        switch (c) {
            case '0':
                return 0;
            case '1':
                return 1;
            case '2':
                return 2;
            case '3':
                return 3;
            case '4':
                return 4;
            case '5':
                return 5;
            case '6':
                return 6;
            case '7':
                return 7;
            case '8':
                return 8;
            case '9':
                return 9;
            case 'a':
            case 'A':
                return 10;
            case 'b':
            case 'B':
                return 11;
            case 'c':
            case 'C':
                return 12;
            case 'd':
            case 'D':
                return 13;
            case 'e':
            case 'E':
                return 14;
            case 'f':
            case 'F':
                return 15;
            default:
                return -1;
        }
    }

    public static String toStringHex(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        try {
            s = new String(baKeyword, StandardCharsets.UTF_8);//UTF-16le:Not
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return s;
    }

    public static int byteArrayToInt(byte[] bytes) {
        int value = 0;
        for (int i = 0; i < bytes.length; i++) {
            int shift = (bytes.length - 1 - i) * 8;
            value += (bytes[i] & 0x000000FF) << shift;
        }
        return value;
    }
}
